package com.itheima.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.constant.MessageConstant;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.entity.Result;
import com.itheima.pojo.CheckItem;
import com.itheima.pojo.Member;
import com.itheima.pojo.OrderSettingList;
import com.itheima.pojo.Setmeal;
import com.itheima.service.MemberService;
import com.itheima.service.OrderSettingListService;
import org.apache.ibatis.annotations.Param;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;
import java.util.Map;

/**
 * @author houzongtao
 * @date 2020/8/30 15:36
 */
@RestController
@RequestMapping("/order")
public class OrderSettingListController {
    @Reference
    private OrderSettingListService orderSettingListService;

    @Reference
    private MemberService memberService;

    //分页查询
    @RequestMapping("/findOrdersPage")
    public PageResult findPage(@RequestBody QueryPageBean queryPageBean){
        PageResult pageResult = orderSettingListService.pageQuery(
                /*queryPageBean.getCurrentPage(),
                queryPageBean.getPageSize(),
                queryPageBean.getQueryString(),
                queryPageBean.getStartDate(),
                queryPageBean.getEndDate(),
                queryPageBean.getOrderType(),
                queryPageBean.getOrderStatus()*/queryPageBean);
        return pageResult;
    }

    @RequestMapping("/submit")
    public Result add(@RequestBody OrderSettingList orderSettingList, Integer setmealId) throws Exception {
            String phoneNumber = orderSettingList.getPhoneNumber();
            Member member = memberService.findByTelephone(phoneNumber);
            if (member == null) {
                //当前用户不是会员，自动完成注册
                member = new Member();
                member.setName(orderSettingList.getMemberName());
                member.setPhoneNumber(phoneNumber);
                member.setRegTime(new Date());
                memberService.add(member);
            }
            Result result = orderSettingListService.add(orderSettingList, setmealId);
            return result;
    }
    //编辑数据回显
    @RequestMapping("/findById")
    public Result findById(Integer id){
        try{
            OrderSettingList orderSettingList = orderSettingListService.findById(id);
            return new Result(true, MessageConstant.QUERY_ORDER_SUCCESS,orderSettingList);
        }catch (Exception e){
            e.printStackTrace();
            //服务调用失败
            return new Result(false, MessageConstant.QUERY_ORDER_FAIL);
        }
    }

    //编辑数据回显
    @RequestMapping("/findSetmealIdByOrderId")
    public Result findSetmealIdByOrderId(Integer id){
        try{
            Integer setmealId = orderSettingListService.findSetmealIdByOrderId(id);
            return new Result(true, MessageConstant.QUERY_SETMEAL_SUCCESS,setmealId);
        }catch (Exception e){
            e.printStackTrace();
            //服务调用失败
            return new Result(false, MessageConstant.QUERY_SETMEAL_FAIL);
        }
    }

    //编辑预约
    @RequestMapping("/edit")
    public Result edit(@RequestBody OrderSettingList orderSettingList, Integer setmealId) throws Exception {
            return  orderSettingListService.edit(orderSettingList,setmealId);
    }

    //删除预约
    @RequestMapping("/delete")
    public Result delete(Integer id){
        try{
            orderSettingListService.delete(id);
            return  new Result(true ,MessageConstant.DELETE_ORDER_SUCCESS);
        }catch (Exception e){
            //删除预约失败
            return new Result(false,MessageConstant.DELETE_ORDER_FALL);
        }
    }

    //操作按钮改变状态
    @RequestMapping("/changeStatus")
    public Result changeStatus(@RequestBody Map map){
        try{
            orderSettingListService.changeStatus(map);
            return  new Result(true ,MessageConstant.UPDATE_ORDERSTATUS_SUCCESS);
        }catch (Exception e){
            //删除预约失败
            return new Result(false,MessageConstant.UPDATE_ORDERSTATUS_FALL);
        }
    }
}
